﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WMS.FC.Domain.PersistenceObject;
using WMS.FC.Infrastructure.Common;

namespace WMS.FC.Infrastructure.POConfig
{
    public abstract class POBaseConfig<T> : IEntityTypeConfiguration<T> where T : POBase
    {
        /// <summary>
        /// 表名
        /// </summary>
        public string TableName { get; protected set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public POBaseConfig()
        {
            TableName = GetTableName();
        }

        /// <summary>
        /// 获取表名
        /// </summary>
        /// <returns></returns>
        private string GetTableName()
        {
            var type = typeof(T);
            var name = type.Name.Replace("PO", string.Empty).ToUpper();
            var nameSpace = type.FullName?.Replace(type.Name, string.Empty) switch
            {
                "WMS.FC.Domain.PersistenceObject.System." => "S_",
                "WMS.FC.Domain.PersistenceObject.Business." => "B_",
                _ => "",
            };

            return $"{nameSpace}{name}";
        }

        /// <summary>
        /// 创建列
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="tableName"></param>
        /// <param name="index"></param>
        public abstract void MapColumn(EntityTypeBuilder<T> builder, ref int index);

        /// <summary>
        /// 创建索引
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="tableName"></param>
        public abstract void MapIndex(EntityTypeBuilder<T> builder);

        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <param name="builder"></param>
        public abstract void InitializeData(EntityTypeBuilder<T> builder);

        /// <summary>
        /// 配置实体
        /// </summary>
        /// <param name="builder"></param>
        public void Configure(EntityTypeBuilder<T> builder)
        {
            var index = 1;
            builder.ToTable(TableName);

            builder.Property(entity => entity.Id).ConfigColumn("ID", "主键").HasColumnOrder(index++);
            MapColumn(builder, ref index);
            builder.Property(entity => entity.CreateDate).ConfigColumn("CREATE_DATE", "创建日期").HasColumnOrder(index++);
            builder.Property(entity => entity.CreatedBy).ConfigColumn("CREATED_BY", "创建人", 50, true).HasColumnOrder(index++);
            builder.Property(entity => entity.ModifyDate).ConfigColumn("MODIFY_DATE", "修改日期").HasColumnOrder(index++);
            builder.Property(entity => entity.ModifiedBy).ConfigColumn("MODIFIED_BY", "修改人", 50).HasColumnOrder(index++);
            builder.Property(entity => entity.ModifyIp).ConfigColumn("MODIFY_IP", "修改人IP", 50).HasColumnOrder(index++);
            builder.Property(entity => entity.IsDeleted).ConfigColumn("IS_DELETED", "是否软删除").HasDefaultValue(0).HasColumnOrder(index++);
            builder.Property(entity => entity.Timestamp).ConfigColumn("TIMESTAMP", "并发令牌").IsRowVersion().HasColumnOrder(index++);

            builder.HasKey(entity => entity.Id).HasName($"PK_{TableName}");
            builder.HasIndex(entity => entity.CreateDate).HasDatabaseName($"IDX_{TableName}_CREATEDATE");
            MapIndex(builder);

            InitializeData(builder);
        }
    }
}
